package com.macro.mall.portal.service.impl;

import cn.hutool.core.date.DateTime;
import com.macro.mall.mapper.BmsFeedbackMapper;
import com.macro.mall.model.*;
import com.macro.mall.portal.dto.BmsFeedBackDetail;
import com.macro.mall.portal.dto.BmsFeedbackParam;
import com.macro.mall.portal.service.BmsFeedBackPicService;
import com.macro.mall.portal.service.BmsFeedBackService;
import com.macro.mall.portal.util.MemberUtil;
import com.macro.mall.portal.util.MemberUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 商品属性管理Service实现类
 * Created by macro on 2018/4/26.
 */
@Service
public class BmsFeedBackServiceImpl implements BmsFeedBackService {

    @Autowired
    private BmsFeedbackMapper bmsFeedbackMapper;

    @Autowired
    private BmsFeedBackPicService bmsFeedBackPicService;

    @Autowired
    private MemberUtil memberUtil;

    @Override
    public int create(HttpServletRequest request, BmsFeedbackParam bmsFeedbackParam) {
        UmsMember member = MemberUtils.getUser();
        //获取当前登录用户id
        BmsFeedback bmsFeedback = new BmsFeedback();
        BeanUtils.copyProperties(bmsFeedbackParam, bmsFeedback);
        bmsFeedback.setCreateTime(new DateTime());
        bmsFeedback.setUpdateTime(new DateTime());
        bmsFeedback.setCreateUser(member.getId());
        bmsFeedback.setUpdateUser(member.getId());
        int count = bmsFeedbackMapper.insertSelective(bmsFeedback);
        List<String> picList = bmsFeedbackParam.getPicList();
        bmsFeedBackPicService.create(bmsFeedback.getId(), picList);
        return count;
    }

    @Override
    public BmsFeedBackDetail getItem(Long id) {
        BmsFeedback bmsFeedback = bmsFeedbackMapper.selectByPrimaryKey(id);
        BmsFeedBackDetail bmsFeedBackDetail = new BmsFeedBackDetail();
        BeanUtils.copyProperties(bmsFeedback, bmsFeedBackDetail);

        BmsFeedbackPicExample example = new BmsFeedbackPicExample();
        example.createCriteria().andBmsFeedbackIdEqualTo(id);
        List<BmsFeedbackPic> bmsFeedbackPics = bmsFeedBackPicService.selectByExample(example);
        List<String> picLists;
        if (CollectionUtils.isNotEmpty(bmsFeedbackPics)) {
            picLists = bmsFeedbackPics.stream().filter(i -> Objects.nonNull(i) && StringUtils.isNotBlank(i.getPic()))
                    .map(BmsFeedbackPic::getPic).collect(Collectors.toList());
            bmsFeedBackDetail.setPicList(picLists);
        }
        return bmsFeedBackDetail;
    }

}
